package com.zlx.mall.biz.attribution.handler;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.zlx.mall.base.entity.BasicPointData;
import com.zlx.mall.basic.common.constant.Constants;
import org.springframework.util.StringUtils;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

/**
 * @author qingke
 * <a href="https://www.cnblogs.com/zimug/p/16580063.html">Doc.</a>
 **/
public class PointDataTableNameHandler implements TableNameHandler {

    private final String filterTableName;
    /**
     * @link BasicPointData
     */
    private static final String POINT_DATA_TABLE_NAME = BasicPointData.TABLE_NAME;
    public static final DateTimeFormatter SUFFIX_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM");

    public PointDataTableNameHandler() {
        this.filterTableName = POINT_DATA_TABLE_NAME;
    }

    public static String getSuffix() {
        return YearMonth.now().format(SUFFIX_FORMATTER);
    }

    @Override
    public String dynamicTableName(String sql, String tableName) {
        if (this.filterTableName.equals(tableName)) {
            return "`" + StringUtils.delete(tableName, "`") + Constants.UNDERLINE + getSuffix() + "`";
        }
        return tableName;
    }
}
